﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using VBFOOL.MediaPlayer.Core.Interface;
namespace VBFOOL.MediaPlayer.MKV
{
    public class MKVFileSource : IInputSource
    {
        public MKVFileSource(LibEBML.Common.Matroska document)
        {
            this.Document = document;
            TimeCodeScale = Document.Segment.Info.TimecodeScale / 100;
        }
        public string Title
        {
            get
            {
                if (Document != null)
                {
                    return Document.Segment.Info.Title;
                }
                else
                {
                    return "";
                }
            }
        }

        public LibEBML.Common.Matroska Document { get; protected set; }
        long TimeCodeScale = 10000;
        public bool BuildStreams()
        {
            Streams.Clear();
            m_Subtitles.Clear();
            foreach (var entry in Document.Segment.Tracks)
            {
                switch (entry.TrackType)
                {
                    case 1:
                        var c = m_Codecs.Where(m => m.SupportCodecID(entry.CodecID)).FirstOrDefault();
                        if (c != null)
                        {
                            MKVMediaStream msv = new MKVMediaStream(this, entry, MediaStreamType.Video, c);
                            Streams.Add(msv);
                        }
                        break;
                    case 2:
                        var c2 = m_Codecs.Where(m => m.SupportCodecID(entry.CodecID)).FirstOrDefault();
                        if (c2 != null)
                        {
                            MKVMediaStream msa = new MKVMediaStream(this, entry, MediaStreamType.Audio, c2);
                            Streams.Add(msa);
                        }
                        break;
                    case 0x11:
                        SimpleSubtitleReader sub = new SimpleSubtitleReader(Document, entry);
                        m_Subtitles.Add(sub);
                        break;
                    default:
                        continue;
                    //break;
                }

            }


            return true;
        }

        List<SimpleSubtitleReader> m_Subtitles = new List<SimpleSubtitleReader>();
        public IEnumerable<ISubtitleReader> Subtitles { get { return m_Subtitles as IEnumerable<ISubtitleReader>; } }

        List<MKVMediaStream> Streams = new List<MKVMediaStream>();
        public IEnumerable<IMediaStream> AvailableStream
        {
            get { return Streams as IEnumerable<IMediaStream>; }
        }
        List<ICodecFilter> m_Codecs = new List<ICodecFilter>();

        public IList<ICodecFilter> Codecs
        {
            get { return m_Codecs; }
        }

        public bool CanSeek
        {
            get { return true; }
        }

        public decimal Duration
        {
            get { return Document.Segment.Info.Duration * TimeCodeScale; }
        }


        public long Seek(long SeekToTime)
        {
            long ms = SeekToTime / TimeCodeScale;
            var cue = Document.Segment.Cues.Where(m => m.CueTime <= ms).LastOrDefault();
            if (cue != null)
            {

            }
            else
            {

            }
            var cls = Document.Segment.Clusters.Where(m => m.TimeCode <= ms).LastOrDefault();
            if (cls != null)
            {
                var i = Document.Segment.Clusters.IndexOf(cls);
                foreach (var item in Streams)
                {
                    item.Seek(i, 0);
                }
                return cls.TimeCode * TimeCodeScale;
            }
            else
            {
                return 0;
            }

            //throw new NotImplementedException();
        }
    }
}
